package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.StockInDetail;
import com.geek.factory.result.Result;
import com.geek.factory.service.IStockInDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入库单明细表控制器
 */
@RestController
@RequestMapping("/stock-in-detail")
@Api(tags = "后台系统的 - 入库单明细模块")
@Slf4j
@CrossOrigin
public class StockInDetailController {

    @Autowired
    private IStockInDetailService stockInDetailService;

    @ApiOperation("通过ID查询入库单明细信息")
    @GetMapping("/{detailId}")
    public Result findStockInDetailById(@ApiParam("明细ID") @PathVariable("detailId") Integer detailId) {
        StockInDetail detail = stockInDetailService.getById(detailId);
        log.info("stockInDetail: {}", detail);
        if (detail != null) {
            return new Result(SuccessConstant.SUCCESS, "ok", detail);
        }
        return new Result(SuccessConstant.FAIL, "明细不存在");
    }

    @ApiOperation("新增入库单明细")
    @PostMapping
    public Result addStockInDetail(@RequestBody StockInDetail detail) {
        boolean isSuccess = stockInDetailService.save(detail);
        log.info("新增入库单明细: {}, 结果: {}", detail, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "新增成功", detail);
        }
        return new Result(SuccessConstant.FAIL, "新增失败");
    }

    @ApiOperation("根据ID删除入库单明细")
    @DeleteMapping("/{detailId}")
    public Result deleteStockInDetail(@ApiParam("明细ID") @PathVariable("detailId") Integer detailId) {
        boolean isSuccess = stockInDetailService.removeById(detailId);
        log.info("删除明细ID: {}, 结果: {}", detailId, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "删除成功");
        }
        return new Result(SuccessConstant.FAIL, "删除失败");
    }

    @ApiOperation("修改入库单明细信息")
    @PutMapping("/{detailId}")
    public Result updateStockInDetail(@ApiParam("明细ID") @PathVariable("detailId") Integer detailId, @RequestBody StockInDetail detail) {
        detail.setDetailId(detailId);
        boolean isSuccess = stockInDetailService.updateById(detail);
        log.info("修改明细: {}, 结果: {}", detail, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "修改成功", detail);
        }
        return new Result(SuccessConstant.FAIL, "修改失败");
    }

    @ApiOperation("分页查询入库单明细列表")
    @GetMapping("/page")
    public Result getStockInDetailPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer stockInId
    ) {
        QueryWrapper<StockInDetail> wrapper = new QueryWrapper<>();
        if (stockInId != null) {
            wrapper.eq("stock_in_id", stockInId);
        }
        wrapper.orderByDesc("detail_id");

        Page<StockInDetail> page = stockInDetailService.page(new Page<>(current, size), wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("records", page.getRecords());
        data.put("total", page.getTotal());
        data.put("current", page.getCurrent());
        data.put("size", page.getSize());
        data.put("pages", page.getPages());

        return new Result(SuccessConstant.SUCCESS, "查询成功", data);
    }

    @ApiOperation("获取所有入库单明细列表")
    @GetMapping("/list")
    public Result getAllStockInDetails() {
        List<StockInDetail> details = stockInDetailService.list();
        return new Result(SuccessConstant.SUCCESS, "查询成功", details);
    }
}
